﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Konyvkolcsonzo
{
    public partial class FmKikolcsonozve : FormKK
    {
        List<kolcson> nevsor;
        Kikolcsonozve k;
        public FmKikolcsonozve()
        {
            InitializeComponent();
            nevsor = new List<kolcson>();
            k = new Kikolcsonozve();
            this.pnlLeft.Controls.Add(txbKeres);
            this.pnlLeft.Controls.Add(lvwKikolcsonozve);
            this.pnlLeft.Controls.Add(btnAdd);
            this.pnlLeft.Controls.Add(btnEdit);
            this.pnlLeft.Controls.Add(btnDelete);
            this.pnlLeft.Controls.Add(btnClose);
            this.pnlLeft.Controls.Add(btnExit);
            this.pnlLeft.Controls.Add(label1);
            this.pnlLeft.Controls.Add(label2);
            this.pnlRight.Controls.Add(btnOk);
            this.pnlRight.Controls.Add(btnCancel);
            this.pnlRight.Controls.Add(cmbFelhasznalok);
            this.pnlRight.Controls.Add(cmbKonyv);
            this.pnlRight.Controls.Add(cmbPeldany);
            this.pnlRight.Controls.Add(label3);
            this.pnlRight.Controls.Add(label4);
            this.pnlRight.Controls.Add(label5);
        }

        private void FmKikolcsonozve_Load(object sender, EventArgs e)
        {
            Populate();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            PopulateLvw();
        }

        private void Populate()
        {
            PopulateLvw();
            PopulatePanelRight();
        }

        private void PopulateLvw()
        {
            lvwKikolcsonozve.Items.Clear();
            nevsor = k.getKikolcsonozve(txbKeres.Text);
            for (int i = 0; i < nevsor.Count; i++)
            {
                ListViewItem sor = new ListViewItem(Convert.ToString(nevsor[i].id));
                sor.SubItems.Add(Convert.ToString(nevsor[i].felID));
                sor.SubItems.Add(Convert.ToString(nevsor[i].nev));
                sor.SubItems.Add(Convert.ToString(nevsor[i].konyvID));
                sor.SubItems.Add(Convert.ToString(nevsor[i].cim));
                sor.SubItems.Add(Convert.ToString(nevsor[i].datum));
                lvwKikolcsonozve.Items.Add(sor);
            }

            if (lvwKikolcsonozve.SelectedItems.Count == 0 && lvwKikolcsonozve.Items.Count > 0)
            {
                lvwKikolcsonozve.Items[0].Selected = true;
            }
        }

        private void PopulatePanelRight()
        {
            cmbFelhasznalok.DataSource = k.getFelhasznalok();
            cmbKonyv.DataSource = k.getKonyvek();
            cmbPeldany.DataSource = k.getPeldanyok(cmbKonyv.SelectedItem.ToString());
            cmbPeldany.Visible = (cmbPeldany.Items.Count != 0);
        }

        private void PopulatePanelRight(long id)
        {
            cmbFelhasznalok.Text = Convert.ToString(lvwKikolcsonozve.SelectedItems[0].SubItems[2].Text);
            cmbKonyv.Text = Convert.ToString(lvwKikolcsonozve.SelectedItems[0].SubItems[4].Text);
            cmbPeldany.Text = Convert.ToString(lvwKikolcsonozve.SelectedItems[0].SubItems[3].Text);
        }

        private void cmbKonyv_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbPeldany.DataSource = k.getPeldanyok(cmbKonyv.SelectedItem.ToString());
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.cmdAdd();
            //PopulatePanelRight();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.cmdEdit(lvwKikolcsonozve.SelectedItems.Count))
            {
                PopulatePanelRight(Convert.ToInt32(lvwKikolcsonozve.SelectedItems[0].SubItems[0].Text));
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.cmdDelete(lvwKikolcsonozve.SelectedItems.Count))
            {
                k.deleteKikolcsonozve(Convert.ToInt32(lvwKikolcsonozve.SelectedItems[0].SubItems[0].Text));
                PopulateLvw();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.cmdClose();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.cmdExit();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.cmdOK();
            kolcson f = new kolcson();
            f.nev = cmbFelhasznalok.Text;
            f.cim = cmbKonyv.Text;
            f.konyvID = Convert.ToInt32(cmbPeldany.Text);

            if (this.editType())
            {
                f.id = Convert.ToInt32(lvwKikolcsonozve.SelectedItems[0].SubItems[0].Text);
                k.updateKikolcsonozve(f);
            }
            else
            {
                f.datum = DateTime.Now.Year + "." + DateTime.Now.Month + "." + DateTime.Now.Day;
                k.insertKikolcsonozve(f);
            }
            PopulateLvw();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.cmdCancel();
        }


    }
}
